package com.qf.controller;

import com.qf.entity.SmsInterfaceForm;
import com.qf.enums.InterfaceExceptionEnums;
import com.qf.service.InterfaceService;
import com.qf.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Ypl
 * @version V1.0
 * @Project OctoberAndNovember
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/11/9 19:18
 */
@SuppressWarnings("all")
@RestController
@Slf4j
public class SmsController {

    @Autowired
    InterfaceService interfaceService;

    //获取响应头
    @Value("${ip-header}")
    private List<String> ipHeader;


    @PostMapping("/sms/interface")
    public Result sendInterface(@RequestBody SmsInterfaceForm smsInterfaceForm, HttpServletRequest request){

        //获取真实的IP
        String ip = getRealIpAddress(ipHeader, request);
        smsInterfaceForm.setIp(ip);

        log.debug("SmsController.sendSMS ：{}" ,smsInterfaceForm);

        //校验信息
        Result result = checkNotNull(smsInterfaceForm);

        //如果数据不为空就返回
        if(result != null){
            return result;
        }


        //和redis中的数据进行校验
        result =interfaceService.checkSmsInterfaceForm(smsInterfaceForm);
        log.debug("发送短信成功");
        return result;
    }

    /**
     * 获取真实的IP
     */
    private String getRealIpAddress(List<String> ipHeader, HttpServletRequest request) {
        if(ipHeader.size() > 0 && ipHeader != null ){
            for (String key : ipHeader) {
                //根据响应头的key获取ip
                String ip = request.getHeader(key);
                if(!StringUtils.isEmpty(ip)){
                    return ip;
                }
            }
        }
        //如果没有获取到IP就返回本机的IP
        return request.getRemoteAddr();
    }

    /**
     * 校验空的方法，如果为空就直接返回，不进入业务层
     * @return
     */

    private Result checkNotNull(SmsInterfaceForm smsInterfaceForm) {

        /**
         * 获取clientID
         */
        String clientID = smsInterfaceForm.getClientID();
        if(StringUtils.isEmpty(clientID)){
            log.error("认证错误，clientID错误");
            //认证错误
            return Result.error(InterfaceExceptionEnums.INTERFACE_CLIENT_ID_ERROR);
        }

        /**
         * 获取SrcID
         */
        String srcID = smsInterfaceForm.getSrcID();
        if(StringUtils.isEmpty(srcID)){
            log.error("下行编号错误");
            return Result.error(InterfaceExceptionEnums.INTERFACE_SRC_ID_ERROR);
        }

        /**
         * 获取 mobile
         */
        String mobile = smsInterfaceForm.getMobile();
        if(StringUtils.isEmpty(mobile)){
            log.error("目的手机号错误");
            return Result.error(InterfaceExceptionEnums.INTERFACE_MOBILE_ERROR);
        }

        /**
         * 获取 content
         *
         */
        String content = smsInterfaceForm.getContent();
        if(StringUtils.isEmpty(content)){
            log.error("发送消息错误");
            return Result.error(InterfaceExceptionEnums.INTERFACE_CONTENT_ERROR);
        }

        /**
         * 获取 pwd
         */
        String pwd = smsInterfaceForm.getPwd();
        if(StringUtils.isEmpty(pwd)){
            log.error("密码错误");
            return Result.error(InterfaceExceptionEnums.INTERFACE_PWD_ERROR);
        }

        /**
         * 获取 ip
         */
        String ip = smsInterfaceForm.getIp();
        if(StringUtils.isEmpty(ip)){
            log.error("IP错误");
            return Result.error(InterfaceExceptionEnums.INTERFACE_IP_ERROR);
        }
        return null;
    }
}
